const {mysql} = require('../../qcloud')

// 获取分类列表
async function categoryNav(ctx){
  const categoryNavs = await mysql('nideshop_category').where({'parent_id':0}).select()
  ctx.state.data = {
    categoryNavs
  }
}

// 头部总分类（ category/category.vue/getNavData 方法）
async function headData(ctx){
  // 取出全部de总分类
  const headNav = await mysql('nideshop_category').where({'parent_id': 0}).select()
  ctx.state.data = {
    headNav
  }
}
//左边子分类（ category/category.vue/getNavData 方法）
async function childData(ctx){
  const{ categoryId } = ctx.query
  const childNav = await mysql('nideshop_category').where({
      'parent_id': categoryId
    }).orderBy("id","asc").select()
  ctx.state.data = {
    childNav
  }
}
//获取当前子分类下的商品信息（ category/category.vue/getNavData 方法）
async function infoData(ctx){
  const { categoryId } = ctx.query
  const infoList = await mysql('nideshop_goods').where({
      'category_id': categoryId
    })
    .column('id', 'name', 'list_pic_url', 'retail_price', 'goods_brief')
    .select()
  ctx.state.data = {
    infoList
  }
}
module.exports = {
  categoryNav,
  headData,
  childData,
  infoData
}